package com.intct.hbase.dml;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.CompareOperator;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;

import java.io.IOException;

/**
 * @author gufg
 * @since 2025-11-04 14:22
 *
 * DependentColumnFilter
 * KeyOnlyFilter
 * ColumnCountGetFilter
 * SingleColumnValueFilter             单列值过滤
 * PrefixFilter                        前缀过滤
 * SingleColumnValueExcludeFilter
 * FirstKeyOnlyFilter
 * ColumnRangeFilter
 * ColumnValueFilter
 * TimestampsFilter
 * FamilyFilter                        列族
 * QualifierFilter                     列限定符
 * ColumnPrefixFilter
 * RowFilter                           rowkey
 * MultipleColumnPrefixFilter
 * InclusiveStopFilter
 * PageFilter                           分页
 * ValueFilter                          值
 * ColumnPaginationFilter
 */
public class ScanDataImpl extends AbsDml{

    public static void main(String[] args) throws IOException {
        new ScanDataImpl().start("intct86", "t2");
    }
    @Override
    void execCommand(Table table) throws IOException {
        // 全表描述
        Scan scan = new Scan();

        // 指定查询多版本
//        scan.readAllVersions();

        // 指定查询版本数量
//        scan.readVersions(3);

        // 指定Rowkey范围 默认：左闭右开，主是startrow和stoprow第二参数控制
//        scan.withStartRow("1001".getBytes());
//        scan.withStopRow("1003".getBytes(), true);

        // 设置limit
//        scan.setLimit(2);


        // 过滤条件

        // rowkey过滤
        RegexStringComparator regexStringComparator = new RegexStringComparator("10.*");
        RowFilter rowFilter = new RowFilter(
                // 比较符
                CompareOperator.EQUAL,
                // 类型匹配
                regexStringComparator
        );

        // 单列值过滤
        SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter(
                // 列族
                "f1".getBytes(),
                // 列限定符
                "name".getBytes(),
                // 比较符
                CompareOperator.EQUAL,
                // 值
                "wangwu".getBytes()
        );

        // 严格过滤
        singleColumnValueFilter.setFilterIfMissing(true);

        // 多个过滤条件，需要创建一个flieterList
        FilterList filterList = new FilterList(rowFilter, singleColumnValueFilter);

        // Scan对象设置过滤
        scan.setFilter(filterList);


        // 查询
        ResultScanner scanner = table.getScanner(scan);

        // 迭代数据
        for (Result result : scanner) {
            // 所有单元格(或者叫字段)
            Cell[] cells = result.rawCells();

            // 根据单元格迭代
            for (Cell cell : cells) {
                // rowkey
                String rowkey = new String(CellUtil.cloneRow(cell));

                // family
                String familyName = new String(CellUtil.cloneFamily(cell));

                // qualifier
                String qualifier = new String(CellUtil.cloneQualifier(cell));

                //value
                String value = new String(CellUtil.cloneValue(cell));

                System.out.println("主键 = " + rowkey + "; 列族 = " + familyName + "; 列限定符 = " + qualifier + "; 值 = " + value);
            }
        }
    }
}
